﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using AbpLearningDemo.Companies.Dto;
using AbpLearningDemo.DataModels;

namespace AbpLearningDemo.Companies
{
    public class CompanyAppService : AbpLearningDemoAppServiceBase, ICompanyAppService
    {
        private readonly IRepository<Company, Guid> _companyRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="companyRepository"></param>
        public CompanyAppService(IRepository<Company, Guid> companyRepository)
        {
            _companyRepository = companyRepository;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<CompanyListDto>> GetCompaniesAsync(GetCompaniesInput input)
        {
            var query = _companyRepository.GetAll();
            query = query.WhereIf(!input.Name.IsNullOrEmpty(), c => c.Name.Contains(input.Name));

            int totalCount = await query.CountAsync();
            List<Company> companies = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();

            return new PagedResultDto<CompanyListDto>(totalCount, companies.MapTo<List<CompanyListDto>>());
        }

        /// <summary>
        /// 单条数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CompanyListDto> GetCompanyAsync(NullableIdDto<Guid> input)
        {
            if (input.Id.HasValue)
            {
                Company company = await _companyRepository.FirstOrDefaultAsync(input.Id.Value);
                return company.MapTo<CompanyListDto>();
            }
            return new CompanyListDto();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<DeleteCompanyResult> DeleteCompanyAsync(EntityDto<Guid> input)
        {
            Company company = await _companyRepository.FirstOrDefaultAsync(input.Id);
            if (company.IsNullOrDeleted())
                return new DeleteCompanyResult("删除的数据不存在");

            company.IsDeleted = true;
            return new DeleteCompanyResult();
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<SaveCompanyResult> InsertCompanyAsync(CompanyEditDto dto)
        {
            Company dbCompany = await _companyRepository.FirstOrDefaultAsync(c => c.Name == dto.Name);
            if (!dbCompany.IsNullOrDeleted())
                return new SaveCompanyResult("已存在相同名称的企业");

            Company company = dto.MapTo<Company>();
            //company.IsActive = true;

            Guid id = await _companyRepository.InsertAndGetIdAsync(company);
            return new SaveCompanyResult(id);
        }

        /// <summary>
        /// 编辑数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<SaveCompanyResult> UpdateCompanyAsync(CompanyEditDto dto)
        {
            Company dbCompany = await _companyRepository.FirstOrDefaultAsync(c => c.Name == dto.Name && c.Id != dto.Id);
            if (!dbCompany.IsNullOrDeleted())
                return new SaveCompanyResult("已存在相同名称的企业");

            Company company = await _companyRepository.FirstOrDefaultAsync(dto.Id);
            if (company.IsNullOrDeleted())
                return new SaveCompanyResult("修改的企业不存在");

            company.Name = dto.Name;
            company.Phone = dto.Phone;
            company.Address = dto.Address;
            company.Introduction = dto.Introduction;
            company.IsActive = dto.IsActive;

            return new SaveCompanyResult(dto.Id);
        }
    }
}
